﻿@page "/autofill"

@inherits ComponentBase

@using Microsoft.Extensions.Localization

@inject IStringLocalizer<Foo> Localizer

<AutoFill TValue="Foo" Items="Items"
            @ref="auto"
            DisplayCount="10"
            NoDataTip="无匹配数据"
            IsLikeMatch="true"
            ShowDropdownListOnFocus="false"
            @onblur="OnBlur"
            OnGetDisplayText="OnGetDisplayText"
            OnSelectedItemChanged="OnSelectedItemChanged"
            OnCustomFilter="OnCustomFilter">
<Template>
    @RenderAutoComplate(context)
</Template>

</AutoFill>


@foreach(var item in CheckBoxItems!)
{
    
}

<NullCheckbox TValue="bool?" @bind-Value="IsCheck" DisplayText="复选框"></NullCheckbox>

<p>选中的值:
    @if (IsCheck.HasValue)
    {
        @IsCheck.Value
    }else
    {
        @:null
    }
</p>

@code {
    bool? IsCheck { get; set; } = null;

    CheckboxState State { get; set; } = CheckboxState.Indeterminate;

    List<Foo>? Items{ get; set; }

    Foo? Model { get; set; }

    AutoFill<Foo>? auto{ get; set; }

    List<CheckBoxItem>? CheckBoxItems { get; set; }

    [Parameter]
    public string? CheckBoxValue{ get; set; }

    [Parameter]
    public EventCallback<string?> CheckBoxValueChanged{ get; set; }

    [Parameter]
    public CheckboxState CheckboxState { get; set; } = CheckboxState.Indeterminate;

    [Parameter]
    public EventCallback<CheckboxState> CheckboxStateChanged{ get; set; }

    protected override void OnInitialized()
    {
        Items = Foo.GenerateFoo(Localizer).Cast<Foo>().ToList();

        CheckBoxItems = new List<CheckBoxItem>();
        CheckBoxItems.Add(new CheckBoxItem()
        {
            Text = "A",
            Value = "1"
        });
        CheckBoxItems.Add(new CheckBoxItem()
        {
            Text = "B",
            Value = "2"
        });
    }

    string OnGetDisplayText(Foo foo) => foo.Name ?? "";

    Task OnBlur()
    {
        if (Model == null)
        {
            OnGetDisplayText(new Foo());
        }
        return Task.CompletedTask;
    }

    Task<IEnumerable<Foo>> OnCustomFilter(string searchText)
    {
        var result = !string.IsNullOrEmpty(searchText)
            ? Items!.Where(x => x.Name!.ToPinYin()!.StartsWith(searchText.ToPinYin(), StringComparison.OrdinalIgnoreCase))
            : Items;
        if (!result!.Any())
        {
            Model = null; 
        }
        return Task.FromResult(result!);
    }

    Task OnSelectedItemChanged(Foo foo)
    {
        Model = foo;
        StateHasChanged();
        return Task.CompletedTask;
    }

    RenderFragment<Foo> RenderAutoComplate = (Foo x) =>
    {
        return
            @<div>
                <div class="ps-2">
                    <div>@x.Name</div>
                    <div class="bb-sub">@x.Id</div>
                </div>
        </div>
    ;
    };

    async Task OnStateChangedAsync(CheckboxState state, string value)
    {
        if (state == CheckboxState.Checked)
        {
            if (!CheckBoxValue.Contains(value + ";"))
            {
                CheckboxState = CheckboxState.Indeterminate;
            }
            CheckBoxValue += value + ";";
        }
        else if (state == CheckboxState.UnChecked)
        {
            CheckBoxValue = CheckBoxValue!.Replace(value + ";", "");
        }
        else if (state == CheckboxState.Indeterminate)
        {
            CheckBoxValue = CheckBoxValue!.Replace(value + ";", "");
        }
        await CheckBoxValueChanged.InvokeAsync(CheckBoxValue);
    }

    async Task StateChanged(CheckboxState state)
    {
        if(state== CheckboxState.Checked)
        {
            await CheckboxStateChanged.InvokeAsync(state);
        }
    }
}
